<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<script>
    // async, await 是 Promise 的语法糖
    function timeout(ms) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(`${ms}ms timeout!`)
                // reject('error')
            }, ms)
        })
    }

    // async function init() {
    const init = async function () {
        // 一旦遇到await，等到一步操作完成之后，再执行await后面的语句
        try {
            await timeout(3000).then(res => {
                console.log(res)
            })
            console.log('time over!!!') // await 后的异步操作完成后执行
            return 123
        } catch (e) {
            console.log('e', e)
        } finally {
            console.log('finally ok!')
        }
        // throw new Error('error')
    }

    init() // async 会返回一个 Promise对象
    /*
    init().then(res => {
        console.log(res)
    }).catch(e => {
        console.log(e)
    })*/

    // console.log(init())


    /*
    const ob1 = {
        // foo: function () {
        foo: async function () {
            const fn = async () => {
                await
            }
        },
        // bar() {
        async bar() {

        }
    }*/

    /*
    // 回掉地狱
    timeout(3000).then(res => {
        console.log(res)
        timeout(3000).then(res => {
            console.log(res)
            timeout(3000).then(res => {
                console.log(res)
            })
        })
    })

    axios({}).then(res => {
        axios({}).then(res => {
            axios({}).then(res => {

            })
        })
    })*/

</script>
</body>
</html>